#include <iostream>
#include <cstring>
#include <fcntl.h>
#include <unistd.h>

bool SetNonBlock(int fd)
{
    int fl = fcntl(fd, F_GETFL);//在底层获取当前fd对应的文件读写标志位
    if(fl<0) return false;
    fcntl(fd,F_SETFL,fl | O_NONBLOCK);//设置非阻塞
    return true;
}

int main()
{
    //0
    SetNonBlock(0);//只要设置一次后续就都是非阻塞了
    char buffer[1024];
    while(true)
    {
        errno = 0;
        ssize_t s = read(0,buffer,sizeof(buffer)-1);
        if(s>0)
        {
            buffer[s-1] = 0;
            std::cout<< "echo# " << buffer << " errno[---]: " << errno <<"errstring"<< strerror(errno)<<std::endl; 
        }
        else
        {
            //todo
            //std::cout<<"read \"error\"" <<" errno: " << errno <<"errstring"<< strerror(errno)<<std::endl; 
            if(errno == EWOULDBLOCK || errno == EAGAIN)
            {
                std::cout<< "当前0号fd没有就绪"<<std::endl;
                continue;
            } 
            else if(errno == EINTR)
            {
                std::cout<< "当前io被信号中断"<<std::endl;
                continue;
            }
            else{
                //差错处理
            }
            sleep(1);
        }
    }
    return 0;
}
    
